@echo off
:: 设置控制台为 UTF-8 编码
chcp 65001 >nul

:: 统一工作目录为脚本所在目录（防止双击时路径错位）
cd /d "%~dp0"

:: 激活虚拟环境
call .venv\Scripts\activate || (
    echo ❌ 虚拟环境激活失败，请检查 .venv 目录！
    pause & exit /b 1
)

echo 🚀 Building...

:: ===== 第 1 步：前端开发模式打包 =====
echo [1/7] Building frontend GUI 〔build mode〕...
cd gui
call pnpm build || (
    echo ❌ pnpm build 执行失败！
    pause & exit /b 1
)
echo ✅ Frontend GUI build complete!
cd .. 

echo [2/7] Running file_version_info files...
python build_file_version_info.py || goto :error
echo ✅ File_version_info files complete!

echo [3/7] Running spec files...
python build_spec.py || goto :error
echo ✅ Spec files complete!

echo [4/7] Running PyInstaller...
pyinstaller main.spec -y || goto :error
echo ✅ PyInstaller complete!

echo [5/7] Running build script...
python package/exe/build.py || goto :error
echo ✅ Build script complete!

echo [6/7] Building installer with Inno Setup...
"package\exe\InnoSetup6\ISCC.exe" "package\exe\InnoSetup.iss" || goto :error
echo ✅ Inno Setup complete!

echo [7/7] Cleaning up...
rmdir /s /q build 2>nul
echo ✅ Clean up complete!

echo ✅ Build complete!
echo ✅ Build complete!
echo ✅ Build complete!
pause
exit /b 0

:error
echo.
echo ❌ 构建失败，请查看上方错误信息！
pause
exit /b 1